from dataclasses import dataclass, field
from typing import List


@dataclass
class Paper:
    """论文数据对象"""
    conference: str
    year: int
    title: str
    authors: List[str] = field(default_factory=list)
    affiliations: List[str] = field(default_factory=list)
    abstract: str = ""
    keywords: List[str] = field(default_factory=list)
    paper_link: str = ""
    doi: str = ""
    track: str = "Research"

    def to_dict(self):
        """将Paper对象转换为字典格式，便于保存到Excel"""
        return {
            'Conference': self.conference,
            'Year': self.year,
            'Title': self.title,
            'Authors': self.authors,
            'Affiliations': self.affiliations,
            'Abstract': self.abstract,
            'Keywords': ', '.join(self.keywords) if self.keywords else '',
            'Paper_Link': self.paper_link,
            'DOI': self.doi,
            'Track': self.track
        }

    def __str__(self):
        return f"{self.title} ({self.conference} {self.year})"


@dataclass
class Conference:
    """会议数据对象"""
    name: str
    year: int
    url: str
    type: str = "dblp"
    track: str = "research"
    papers: List[Paper] = field(default_factory=list)

    def add_paper(self, paper: Paper):
        """向会议添加论文"""
        self.papers.append(paper)

    def get_papers_count(self):
        """获取会议论文数量"""
        return len(self.papers)

    def __str__(self):
        return f"{self.name} {self.year} ({self.get_papers_count()} 篇论文)"
